package com.boot.springbootdemo;

import ch.qos.logback.core.db.DBHelper;
import com.boot.springbootdemo.bean.Pet;
import com.boot.springbootdemo.bean.User;
import com.boot.springbootdemo.myconfig.MyConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

//@ComponentScan(basePackages = "")  指定包扫描  或 @SpringBootApplication(scanBasePackages = "")
@SpringBootApplication
public class SpringbootdemoApplication extends WebMvcConfigurationSupport {

    public static void main(String[] args) {
        //TODO 1
        //1、返回ioc容器
        ConfigurableApplicationContext run = SpringApplication.run(SpringbootdemoApplication.class, args);
        //2、查看容器里面的组件
        String[] names = run.getBeanDefinitionNames();
        System.out.println("=============容器开始===========");
        for (String name : names) {
            System.out.println(name);
        }
        System.out.println("=============容器结束===========");

        //3、从容器中获取组件
        User user01 = run.getBean("user01", User.class);
        User user02 = run.getBean("user01", User.class);
        System.out.println("组件user01=" + (user01 == user02));

        //4com.boot.springbootdemo.myconfig.MyConfig$$EnhancerBySpringCGLIB$$129b8b91@2c30b71f
        MyConfig bean = run.getBean(MyConfig.class);
        System.out.println(bean);

        //TODO 2  @Configuration(proxyBeanMethods = false) 组件不被依赖 非单例
        //如果@Configuration(proxyBeanMethods = true)代理对象调用方法
        //保证组件单实例
        Pet cat = bean.cat();
        Pet cat1 = bean.cat();
        System.out.println("组件cat=" + (cat1 == cat));

        //TODO 3  @Import  用于引入组件
        String[] beanNamesForType = run.getBeanNamesForType(User.class);
        System.out.println("多个相同类型组件");
        for (String s : beanNamesForType) {
            System.out.println(s);
        }
        //com.boot.springbootdemo.bean.User 全限定类名
        //user01
        DBHelper bean1 = run.getBean(DBHelper.class);
        System.out.println(bean1);

        //TODO 4 @Conditional
        boolean user011 = run.containsBean("user01");
        System.out.println("容器中是否存在===>"+user011);


    }


}
